//
//  DSVisitorView.swift
//  weibo
//
//  Created by apple on 16/7/19.
//  Copyright © 2016年 小环. All rights reserved.
//

import UIKit

class DSVisitorView: UIView {
    
    weak var visitorViewDelegate:DSVisitorViewDelegate?
    
    //初始化访客view
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor(white: 237/256.0, alpha: 1)
        //准备访客控件
        prepareUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*********************懒加载控件*********************/
    //MARK: - 转轮
    private lazy var iconView:UIImageView=UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //MARK: - 房子
    private lazy var homeView:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //MARK: - 遮罩
    private lazy var coverView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    //MARK: - 文字标签
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text="关注一些人,看看有上面惊喜"
        label.textColor=UIColor.darkGrayColor()
        label.numberOfLines=0
        label.sizeToFit()
        
        return label
    }()
    
    //MARK: - 注册按钮
    private lazy var registerButton:UIButton = {
       let button=UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加绑定事件
        button.addTarget(self, action: "visitorViewRegister", forControlEvents: UIControlEvents.TouchDown)
        
        return button
    }()
    
    //MARK: - 登录按钮
    private lazy var loginButton:UIButton = {
       let button=UIButton()
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加绑定事件
        button.addTarget(self, action: "visitorViewLogin", forControlEvents: UIControlEvents.TouchDown)
        
        
        return button
    }()
    
    //MARK: - 准备访客控件
    private func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
     /*********************控件约束*********************/
        //关闭Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints=false
        homeView.translatesAutoresizingMaskIntoConstraints=false
        messageLabel.translatesAutoresizingMaskIntoConstraints=false
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        loginButton.translatesAutoresizingMaskIntoConstraints=false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        
        //iconView约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //homeView
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //MessageLabel
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        //registerButton
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
         addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //loginButton
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //coverImageView
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    /**
     方便不同的控制器调用，显示不同的界面
     
     - parameter imageName: 替换中间的图片
     - parameter message:   提示文本
     */
    func setupVisitorInfo(imageName:String,message:String){
        iconView.image=UIImage(named: imageName)
        messageLabel.text=message
        messageLabel.sizeToFit()
        //隐藏小房子
        homeView.hidden=true
        //遮罩放到最底下
        sendSubviewToBack(coverView)
    }
    /**
     旋转动画
     */
    func startRotationAnimation(){
        let animation=CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration=20
        animation.repeatCount=MAXFLOAT
        animation.removedOnCompletion=false
        
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    
    //MARK: - 点击事件
    func visitorViewRegister(){
        //调用代理
        visitorViewDelegate?.visitorViewRegister()
    }
    
    func visitorViewLogin(){
        visitorViewDelegate?.visitorViewLogin()
    }
}
/**
 *  创建代理协议
 */
protocol DSVisitorViewDelegate:NSObjectProtocol{
    //点击注册
    func visitorViewRegister()
    //点击登录
    func visitorViewLogin()
}
